using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Com.FirstSolver.Splash;

public static class JsonHelper
{
	public static string ToJsonString(this object item)
	{
		using MemoryStream ms = new MemoryStream();
		DataContractJsonSerializer ser = new DataContractJsonSerializer(item.GetType());
		ser.WriteObject((Stream)ms, item);
		return Encoding.UTF8.GetString(ms.ToArray());
	}

	public static T FromJsonTo<T>(this string jsonString)
	{
		using MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
		DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
		return (T)ser.ReadObject((Stream)ms);
	}
}
